package com.gexiaobao.pigeon.app.util

import android.text.TextUtils
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.model.bean.AddressListResponse
import com.gexiaobao.pigeon.app.model.bean.MapLocationBean
import com.gexiaobao.pigeon.app.model.bean.UserInfoResponse
import com.gexiaobao.pigeon.app.model.param.AddDrippingParam
import com.gexiaobao.pigeon.app.model.param.BloodIntentDataBean
import com.google.gson.Gson
import com.tencent.mmkv.MMKV

object CacheUtil {
    /**
     * 获取保存的账户信息
     */
    fun getUser(): UserInfoResponse? {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("user")
        return if (TextUtils.isEmpty(userStr)) {
            null
        } else {
            Gson().fromJson(userStr, UserInfoResponse::class.java)
        }
    }

    /**
     * 设置账户信息
     */
    fun setUser(userResponse: UserInfoResponse?) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("user", "")
            setIsLogin(false)
        } else {
            kv.encode("user", Gson().toJson(userResponse))
            setIsLogin(true)
        }
    }

    /**
     * 获取保存的收货地址信息
     */
    fun getAddressInfo(): AddressListResponse.ListBean? {
        val kv = MMKV.mmkvWithID("app")
        val addressStr = kv.decodeString("address")
        return if (TextUtils.isEmpty(addressStr)) {
            null
        } else {
            Gson().fromJson(addressStr, AddressListResponse.ListBean::class.java)
        }
    }

    /**
     * 设置收货地址信息
     */
    fun setAddressInfo(addressResponse: AddressListResponse.ListBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (addressResponse == null) {
            kv.encode("address", "")
            setIsLogin(false)
        } else {
            kv.encode("address", Gson().toJson(addressResponse))
        }
    }

    /**
     * 获取保存的地图选点信息
     */
    fun getMapLocationBean(): MapLocationBean? {
        val kv = MMKV.mmkvWithID("app")
        val addressStr = kv.decodeString("mapAddress")
        return if (TextUtils.isEmpty(addressStr)) {
            null
        } else {
            Gson().fromJson(addressStr, MapLocationBean::class.java)
        }
    }

    /**
     * 设置地图选点信息
     */
    fun setMapLocationBean(bean: MapLocationBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (bean == null) {
            kv.encode("mapAddress", "")
            setIsLogin(false)
        } else {
            kv.encode("mapAddress", Gson().toJson(bean))
        }
    }

    /**
     * 保存赛鸽信息（滴滴接鸽发起订单）
     */
    fun setDrippingPigeonList(list: ArrayList<AddDrippingParam.PigeonInfoBean>?) {
        val kv = MMKV.mmkvWithID("app")
        if (list.isNullOrEmpty()) {
            kv.encode("drippingPigeonList", "")
        } else {
            kv.encode("drippingPigeonList", Gson().toJson(list))
        }
    }

    /**
     * 获取保存的弟弟接鸽订单中的鸽子
     */
    fun getDrippingPigeonList(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("drippingPigeonList").toString()
    }

    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("login", false)
    }

    /**
     * 设置是否已经登录
     */
    fun setIsLogin(isLogin: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("login", isLogin)
    }

    /**
     * 是否是第一次打开app
     */
    fun isFirst(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("first", true)
    }

    /**
     * 是否是第一次打开app
     */
    fun setFirst(first: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("first", first)
    }

    /**
     * 是否是第一次请求地址信息
     */
    fun isFirstAddress(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("firstAddress", true)
    }

    /**
     * 是否是第一次请求地址信息
     */
    fun setFirstAddress(first: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("firstAddress", first)
    }

    fun getToken(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString(Constant.TOKEN).toString()
    }

    fun setToken(token: String): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode(Constant.TOKEN, token).toString()
    }

    fun getLanguage(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString(Constant.LANGUAGE).toString()
    }

    fun setLanguage(language: String): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode(Constant.LANGUAGE, language).toString()
    }

    fun getLanguageApp(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString(Constant.LANGUAGE_APP).toString()
    }

    fun setLanguageApp(languageApp: String): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode(Constant.LANGUAGE_APP, languageApp).toString()
    }

    /** 保存手势密码开关 */
    fun getPatternTurn(): Int {
        return KvUtils.decodeInt(Constant.PATTERN_TURN)!!
    }

    fun setPatternTurn(value: Int) {
        KvUtils.encode(Constant.PATTERN_TURN, value)
    }

    /**  */
    fun getPattern(): Int {
        return KvUtils.decodeInt(Constant.PATTERN)!!
    }

    fun setPattern(value: Int) {
        KvUtils.encode(Constant.PATTERN, value)
    }

}